package com.example.hujiansong.caculate;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {
    // 数字按钮
    private Button mBtn_1;
    private Button mBtn_2;
    private Button mBtn_3;
    private Button mBtn_4;
    private Button mBtn_5;
    private Button mBtn_6;
    private Button mBtn_7;
    private Button mBtn_8;
    private Button mBtn_9;
    private Button mBtn_0;
    private Button mBtn_add;
    private Button mBtn_sub; // 减
    private Button mBtn_multiply; // 乘
    private Button mBtn_divide; // 除
    private Button mBtn_del;// 删除一个
    private Button mBtn_equals; // 等于
    private Button mBtn_point; // 点
    private Button mBtn_clean; // 清除
    private Button mBtn_divide100; // %

    private EditText mEdt_play; // 显示运算过程
    private TextView mTv_result;// 显示结果

    private StringBuffer buffer = new StringBuffer();
    private String TAG = "cal";

    private boolean flag = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        initEvent();
    }

    private void initEvent() {
        mBtn_0.setOnClickListener(this);
        mBtn_1.setOnClickListener(this);
        mBtn_2.setOnClickListener(this);
        mBtn_3.setOnClickListener(this);
        mBtn_4.setOnClickListener(this);
        mBtn_5.setOnClickListener(this);
        mBtn_6.setOnClickListener(this);
        mBtn_7.setOnClickListener(this);
        mBtn_8.setOnClickListener(this);
        mBtn_9.setOnClickListener(this);
        mBtn_add.setOnClickListener(this);
        mBtn_sub.setOnClickListener(this);
        mBtn_multiply.setOnClickListener(this);
        mBtn_divide.setOnClickListener(this);
        mBtn_point.setOnClickListener(this);
        mBtn_equals.setOnClickListener(this);
        mBtn_clean.setOnClickListener(this);
        mBtn_del.setOnClickListener(this);
        mBtn_divide100.setOnClickListener(this);
    }

    private void initView() {
        mBtn_0 = findViewById(R.id.btn_0);
        mBtn_1 = findViewById(R.id.btn_1);
        mBtn_2 = findViewById(R.id.btn_2);
        mBtn_3 = findViewById(R.id.btn_3);
        mBtn_4 = findViewById(R.id.btn_4);
        mBtn_5 = findViewById(R.id.btn_5);
        mBtn_6 = findViewById(R.id.btn_6);
        mBtn_7 = findViewById(R.id.btn_7);
        mBtn_8 = findViewById(R.id.btn_8);
        mBtn_9 = findViewById(R.id.btn_9);
        mBtn_add = findViewById(R.id.btn_calc_add);
        mBtn_sub = findViewById(R.id.btn_sub);
        mBtn_multiply = findViewById(R.id.btn_multiply);
        mBtn_divide = findViewById(R.id.btn_divide);
        mBtn_divide100 = findViewById(R.id.btn_calc_divide100);
        mBtn_del = findViewById(R.id.btn_del);
        mBtn_equals = findViewById(R.id.btn_equal);
        mBtn_point = findViewById(R.id.btn_calc_doat);
        mBtn_clean = findViewById(R.id.btn_clear);
        mEdt_play = findViewById(R.id.edt_calc);
        mEdt_play.setText("");
        mTv_result = findViewById(R.id.txt_calc);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_0:
                updateData("0");
                break;
            case R.id.btn_1:
                updateData("1");
                break;
            case R.id.btn_2:
                updateData("2");
                break;
            case R.id.btn_3:
                updateData("3");
                break;
            case R.id.btn_4:
                updateData("4");
                break;
            case R.id.btn_5:
                updateData("5");
                break;
            case R.id.btn_6:
                updateData("6");
                break;
            case R.id.btn_7:
                updateData("7");
                break;
            case R.id.btn_8:
                updateData("8");
                break;
            case R.id.btn_9:
                updateData("9");
                break;
            case R.id.btn_calc_doat:
                updateData(".");
                break;
            case R.id.btn_del:
                flag = true;
                updateData("d");
                break;
            case R.id.btn_calc_add:
                updateData("+");
                break;
            case R.id.btn_sub:
                updateData("-");
                break;
            case R.id.btn_multiply:
                updateData("×");
                break;
            case R.id.btn_divide:
                updateData("/");
                break;
            case R.id.btn_calc_divide100:
                updateData("%");
                break;
            case R.id.btn_equal:
                updateData("=");
                break;
            case R.id.btn_clear:
                Log.e(TAG, "clear click");
                buffer = new StringBuffer();
                mTv_result.setText("0");
                mEdt_play.setText("");
                flag = true;
                break;
        }
    }

    private void updateData(String ch) {
        if (flag) {
            if ("d".equals(ch)) {
                int len = buffer.toString().length();
                if (len == 0) {
                    return;
                }

                String newCalLine = buffer.substring(0, len - 1);
                buffer = new StringBuffer(newCalLine);

            } else if ("=".equals(ch)) {
                String calStr = mEdt_play.getText().toString();
                if (calStr == null || "".equals(calStr)) {
                    mTv_result.setText("Error");
                    return;
                }
                String result = "";
                try {
                    result = Calculator.do_calculate(calStr);
                    Log.e(TAG, "result[" + result + "]");
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage() + "");
                    return;
                }
                buffer = new StringBuffer(mEdt_play.getText().toString());
                if (result == null || "".equals(result)) {
                    mTv_result.setText("Error");
                    return;
                } else {
                    mTv_result.setText("" + result);
                }
            } else {
                if (".".equals(ch)) {
                    int len = buffer.toString().length();
                    String lastNum;
                    if(len>0){
                        lastNum  = buffer.substring(len - 1, len);
                        if (!lastNum.contains(".")) {
                            if ("".equals(lastNum)) {
                                buffer.append("0.");
                            } else {
                                buffer.append(".");
                            }
                        }
                    }else{
                        buffer.append("0.");
                    }
                } else {
                    buffer.append("" + ch);
                }

            }
            mEdt_play.setText(buffer.toString());
        }

    }

}
